#include <QProgressBar>
#include <QFileDialog>
#include <QDesktopServices>
#include "yumsyncwindow.h"
#include "qtstdbridge.h"
#include "ui_yumsyncwindow.h"

YumSyncWindow::YumSyncWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::YumSyncWindow)
{
    fileList = new QStringList();
    mtp = NULL;
    ui->setupUi(this);
}

YumSyncWindow::~YumSyncWindow()
{
    if (mtp) {
        mtp->disconnect();
        delete mtp;
    }
    delete ui;
}

void
YumSyncWindow::findMp3Files(QString path)
{
    QDir *currentDir = new QDir(path);
    QFileInfoList childFiles = currentDir->entryInfoList(QStringList("*.mp3"), QDir::Files);
    foreach (QFileInfo child, childFiles) {
        fileList->append(child.absoluteFilePath());
    }

    QFileInfoList childDirs = currentDir->entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
    foreach (QFileInfo child, childDirs) {
        QString absolutePath = child.absoluteFilePath();
        findMp3Files(absolutePath);
    }
}

void
YumSyncWindow::fillTableWithTracks() {
    for (int i=0; i< fileList->size(); i++) {
        QString fileName = fileList->at(i);
        Track track = YBMtpSync::extractTrackInfo(fileName.toStdString());

        int row = ui->filesTable->rowCount();
        ui->filesTable->insertRow(row);
        fillRowWithTrack(row, track);
    }
    ui->filesTable->resizeColumnsToContents();
}

void
YumSyncWindow::fillRowWithTrack(int row, Track track) {
    QString title = QString::fromStdString(track.title);
    QString length = tr("%1 MS").arg(track.length);
    QString artist = QString::fromStdString(track.artist);
    QString album = QString::fromStdString(track.album);
    QString filename = QString::fromStdString(track.filename);

    QTableWidgetItem *titleItem = new QTableWidgetItem(title);
    titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
    ui->filesTable->setItem(row, 0, titleItem);

    QTableWidgetItem *lengthItem = new QTableWidgetItem(length);
    lengthItem->setFlags(lengthItem->flags() ^ Qt::ItemIsEditable);
    ui->filesTable->setItem(row, 1, lengthItem);

    QTableWidgetItem *artistItem = new QTableWidgetItem(artist);
    artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
    ui->filesTable->setItem(row, 2, artistItem);

    QTableWidgetItem *albumItem = new QTableWidgetItem(album);
    albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
    ui->filesTable->setItem(row, 3, albumItem);

    QTableWidgetItem *filenameItem = new QTableWidgetItem(filename);
    filenameItem->setFlags(filenameItem->flags() ^ Qt::ItemIsEditable);
    ui->filesTable->setItem(row, 4, filenameItem);
}

void
YumSyncWindow::updateTableWithSyncInfo() {
    QStringList *toBeAdded = new QStringList();

    for (unsigned int i=0; i<syncInfo.toBeAdded.size(); i++) {
        Track track = syncInfo.toBeAdded[i];
        QString fileName = QString::fromStdString(track.filename);
        toBeAdded->append(fileName);
    }

    for (int row=0; row<ui->filesTable->rowCount(); row++) {
        QTableWidgetItem *item = ui->filesTable->item(row, 4);
        QString fileName = (QString) item->text();
        QString status = "Added";
        if (toBeAdded->contains(fileName)) {
            status = "To Be Added";
        }

        QTableWidgetItem *statusItem = new QTableWidgetItem(status);
        statusItem->setFlags(statusItem->flags() ^ Qt::ItemIsEditable);
        ui->filesTable->setItem(row, 5, statusItem);
    }

    for (unsigned int i=0; i<syncInfo.toBeDeleted.size(); i++) {
        Track track = syncInfo.toBeDeleted[i];
        int row = ui->filesTable->rowCount();
        ui->filesTable->insertRow(row);
        fillRowWithTrack(row, track);

        QTableWidgetItem *statusItem = new QTableWidgetItem("To Be Deleted");
        statusItem->setFlags(statusItem->flags() ^ Qt::ItemIsEditable);
        ui->filesTable->setItem(row, 5, statusItem);
    }
}

void
YumSyncWindow::on_txtPath_returnPressed()
{
    QString sInputDir = ui->txtPath->text();
    //Clear the file list
    fileList->clear();
    //clear the Table items
    for (int i=ui->filesTable->rowCount() -1; i>=0; i--) {
        ui->filesTable->removeRow(i);
    }

    findMp3Files(sInputDir);
    fillTableWithTracks();
}

void
YumSyncWindow::on_btnBrowse_clicked()
{
    QString path =  ui->txtPath->text();
    if (path.isEmpty()) {
        path = "./";
    }
    QString sInputDir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                 path,
                                                 QFileDialog::ShowDirsOnly
                                                 | QFileDialog::DontResolveSymlinks);

    ui->txtPath->setText(sInputDir);
    on_txtPath_returnPressed();
}

void
YumSyncWindow::on_btnSync_clicked()
{
    QString path = ui->txtPath->text();
    mtp->synchronizeDevice(path.toStdString(), syncInfo);
}

void YumSyncWindow::on_btnSyncPreview_clicked()
{
    if (mtp != NULL) {
        mtp->disconnect();
        delete mtp;
        mtp = NULL;
    }

    mtp = new YBMtpSync();
    mtp->connect();
    std::vector<std::string> filesVector;
    for (int i=0; i<fileList->size(); i++) {
        QString name = fileList->at(i);
        filesVector.push_back(name.toStdString());
    }

    syncInfo =  mtp->getSynchronizeDeviceInfo(filesVector);
    updateTableWithSyncInfo();
}

